package me.helllp.dake.iam.core.rest;

import lombok.extern.slf4j.Slf4j;
import me.helllp.dake.common.DakeConst;
import me.helllp.dake.common.MessageCode;
import me.helllp.dake.common.base.BaseController;
import me.helllp.dake.common.base.BaseResult;
import me.helllp.dake.common.exception.DakeAuthException;
import me.helllp.dake.common.exception.DakeEncryptException;
import me.helllp.dake.common.exception.DakeException;
import me.helllp.dake.core.bean.DakeAuthAccount;
import me.helllp.dake.core.utils.AccountUtil;
import me.helllp.dake.core.utils.MessageUtils;
import me.helllp.dake.iam.core.DakeIamCoreConfig;

import me.helllp.dake.iam.core.dto.AuthDto;
import me.helllp.dake.iam.core.dto.LoginDto;
import me.helllp.dake.iam.core.service.IAuthService;
import me.helllp.dake.iam.core.vo.LoginVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * 账号登录相关操作接口
 */
@RestController
@RequestMapping("/iam")
@Slf4j
public class DakeIamController extends BaseController {
    @Autowired
    private IAuthService authService;

    /**
     * 登录接口
     *
     * @param loginDto
     * @return
     * @throws DakeException
     */
    @PostMapping("/login")
    public BaseResult<LoginVo> login(@RequestBody LoginDto loginDto) throws DakeException {
        LoginVo vo = authService.login(loginDto);
        return BaseResult.result(vo.getCode(), MessageUtils.message(vo.getCode()), vo);
    }

    @PostMapping("/verify")
    public BaseResult<DakeAuthAccount> verifyToken(@RequestHeader(value= DakeConst.AUTH_KEY) String token) throws DakeAuthException {
        Assert.notNull(token, MessageUtils.message(MessageCode.MSG_NO_TOKEN));
        DakeAuthAccount authUser = authService.verifyToken(token);
        return BaseResult.success(authUser);
    }

    @PostMapping("/logout")
    public BaseResult<Boolean> logout(@RequestHeader(value= DakeConst.AUTH_KEY) String token) throws DakeAuthException {
        Assert.notNull(token, MessageUtils.message(MessageCode.MSG_NO_TOKEN));
        Boolean isSuccess = authService.logout(token);
        return BaseResult.success(isSuccess);
    }

    @PostMapping("/verifyCode")
    public BaseResult<String> genVerifyCode(@RequestBody LoginDto loginDto) {
        Assert.notNull(loginDto, MessageUtils.message(MessageCode.ERROR_INPUT_PARAMETER));
        Assert.notNull(loginDto.getAccount(), MessageUtils.message(MessageCode.ERROR_INPUT_PARAMETER));
        Assert.notNull(loginDto.getTenantId(), MessageUtils.message(MessageCode.ERROR_INPUT_PARAMETER));
        String pic = authService.genVerifyCode(loginDto.getTenantId(), loginDto.getAccount());
        return BaseResult.success(pic);
    }

    /**
     * 鉴权接口
     *
     * @param authDto
     * @return
     */
    @PostMapping("/auth")
    public BaseResult<Boolean> auth(@RequestBody AuthDto authDto) {
        DakeAuthAccount account = AccountUtil.toAuthAccount();
        Assert.notNull(account, MessageUtils.message(MessageCode.MSG_AUTH_NOT_LOGIN));
        return BaseResult.success(authService.auth(account, authDto));
    }

}
